package com.gitee.ricesugar.controller;

import cn.hutool.crypto.SecureUtil;
import com.gitee.ricesugar.common.Result;
import com.gitee.ricesugar.controller.request.AdminPageRequest;
import com.gitee.ricesugar.entity.Admin;
import com.gitee.ricesugar.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    private static final String PASSWORD_SALT = "RiceSugar";

    @PostMapping("/add")
    public Result add(@RequestBody Admin obj){
        obj.setPassword(SecureUtil.md5(obj.getPassword() + PASSWORD_SALT));
        adminService.add(obj);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(AdminPageRequest obj){
        return Result.success(adminService.page(obj));
    }

    @GetMapping("/{uid}")
    public Result getByUid(@PathVariable String uid){
        return Result.success(adminService.getByUid(uid));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin obj){
        adminService.update(obj);
        return Result.success();
    }

    @DeleteMapping("/delete/{uid}")
    public Result deleteByUid(@PathVariable String uid){
        adminService.deleteByUid(uid);
        return Result.success();
    }
}
